<?php
namespace Test\Controller;
use Common\Controller\AdminbaseController;
/*
 * 后台控制器模板
 * */
class TestController extends AdminbaseController{
	protected $model_model;
	function _initialize() {
		Vendor('PingppSDK.init');
		parent::_initialize();
		//$this->model_model = D("Common/model");
	}
	
	/*表单 模板*/
	function index(){
		$this->display();
	}
	/*文本编辑器*/
	function editor(){
		$this->display();
	}
	/*多表查询*/
	function search(){
		$this->display();
	}
	function down(){
		$this->_export_excle();
	}
	/*ping++ pay*/
	function pay(){
		$result = pingpppay($channel,100*$amount, $order_no);
		$this->success(json_encode($result['data'],true));
	}
	/*统计图*/
	function statistics(){
		$this->display();
	}
	/*map*/
	function map(){
		$this->display();
	}
	
	/*微信消息发送测试*/
	function get_wechat(){
		$wechat=$this->getWechat();
		var_dump($this->getWechat());
	}
	
	/*表单验证*/
	function varify(){
		$this->display();
	}
	function uniq_test(){
		  $data['message']=true;
		  $this->ajaxDefaultReturn($data);
	}
	//////////////////////////////////////api 测试////////////////////////////////
		/*api 测试*/
		function api(){
		$param=trim(I('url'));
		$this->assign('url',$param);
		if(empty($param)){
			$this->display();
			return;
		}
		if(strpos($param, 'http:')!==false){
		}else{
			$param="http://localhost/ocmphp/".$param;
		}
		$this->assign('result',go_curl($param));
		$this->display();
	}
		
	function tem(){
		$this->ajaxReturn(sp_ajax_return('success', 'success', 1));
	}
	/*测试接口*/
	function visit(){
		$test_count=S("test_count");
		if(empty($test_count)){
			$test_count=1;
		}else{
			$test_count+=1;
		}
		S("test_count",$test_count);
		header("Content-type:text/html;charset=utf-8");
		var_dump("您好！你的ip是".get_client_ip()." 您是第" .S("test_count")."位访客");
	}
	
	/*微信操作*/
	function wx_auth(){
		$this->_wxoauth();
		$this->display();
	}
	
	//////////////////////////////////////api 测试////////////////////////////////
	/*================================================================================================*/
	/*统计图数据*/
	function statistics_data(){
		$data[]=array('Firefox', 45.0);
		$data[]=array('IE', 26.8);
		$data[]=array('Chrome', 12.8,true,true);
		$data[]=array('Safari', 8.5);
		$data[]=array('Opera', 6.2);
		$data[]=array('Others',  0.7);
		
//		$data['counts']['others_count']=18;
//		$data['count']=100;
//		$data['sex']=100;
//		$data['sexs'][]=80;
//		$data['sexs'][]=20;
		$this->success($data);
	}
	
	/*列表数据查询*/
	function _list(){
		/*要查询的字段*/
		$fields = array('a_job_no' => array("field" => "a.job_no", "operator" => "like"),
		                'a_name' => array("field" => "a.name", "operator" => "like"), 
		                'b_name' => array("field" => "b.name", "operator" => "like"),
		                'a_place' => array("field" => "a.place", "operator" => "like"),
		                'd_com_name' => array("field" => "d.name", "operator" => "like"),
		                'a_city' => array("field" => "a.city", "operator" => "like"),
		                'a_status' => array("field" => "a.status", "operator" => "like")
						);
		/*查询语句条件*/				
		$where_and = $this -> get_where_ands(IS_POST, $fields);
		/*通过and 拼接条件*/
		$where_and=join('and', $where_and);
		/*统计数量*/
     	$count = $this -> model_model -> field('1') -> alias('a') 
		-> join(array('LEFT JOIN m_job_type AS b ON a.type_id = b.id','LEFT JOIN m_job_status AS c ON a.job_no = c.job_no','LEFT JOIN m_company AS d ON d.com_no = a.com_no')) 
		-> where($where) -> count();
		/*生成page*/
		$page=$this->page($count,20);
		$this->assign('Page',$page->show('Admin'));
		$this->assign('current_page',$page->GetCurrentPage());
		/*列表页显示的字段*/
		$fields=" a.status,a.top_weight,a.create_time,a.emp_num_notsure, a.city AS city_name,a.id,a.job_no,a.name,b.name as job_type,a.place,a.emp_num,a.finish_num,d.name as com_name,c.have_seen_num,c.working_num";
		/*数据*/
		$list = $this -> model_model -> field($fields)-> alias('a')
 		->join(array('LEFT JOIN m_job_type AS b ON a.type_id = b.id','LEFT JOIN m_job_status AS c ON a.job_no = c.job_no','LEFT JOIN m_company AS d ON d.com_no = a.com_no'))
 		-> where($where) -> order('a.top_weight desc ,a.status, a.create_time desc') -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('list', $list);
	}

		/*导出excle*/
	private function _export_excle() {
		$fields = array('com_no' => array("field" => "com_no", "operator" => "like"), 'contract_person' => array("field" => "contract_person", "operator" => "like"), 'tel' => array("field" => "tel", "operator" => "like"), 'name' => array("field" => "name", "operator" => "like"), 'check_result' => array("field" => "check_result", "operator" => "like"), 'industry' => array("field" => "industry", "operator" => "like"));
		$check_result = "case check_result  WHen 0 THEN '未审核' 	WHEN 1 THEN '审核通过' when 2 THEN '审核不通过' END as check_result";
		$data = $this -> model_model -> field("com_no,name,num,addr,contract_person,tel,email,industry,create_time,check_time,$check_result") -> where($where) -> select();
		$titles_des = array('编号', '名称', '关注人数', '地址', '联系人', '联系电话', '邮箱', '所属行业', '创建时间', '审核时间', '审核状态');
		$titles_field = array('com_no', 'name', 'num', 'addr', 'contract_person', 'tel', 'email', 'industry', 'create_time', 'check_time', 'check_result');
		$title = "商家列表";
		export_excel($titles_des, $titles_field, $data, $title);
	}
	public function _wxoauth(){
		$we_obj = $this->getWechat();
		$scope = 'snsapi_base';
		$code = isset($_GET['code'])?$_GET['code']:'';
		$token_time = isset($_SESSION['token_time'])?$_SESSION['token_time']:0;
		if(!$code && isset($_SESSION['open_id']) && isset($_SESSION['user_token']) && $token_time>time()-3600)
		{
			if (!$this->wxuser) {
				$this->wxuser = $_SESSION['wxuser'];
			}
			$this->open_id = $_SESSION['open_id'];
			return $this->open_id;
		}
		else
		{
			$options = array(
					'token'=>$this->options["token"], //填写你设定的key
					'appid'=>$this->options["appid"], //填写高级调用功能的app id
					'appsecret'=>$this->options["appsecret"] //填写高级调用功能的密钥
			);
			
			if ($code) {
				$json = $we_obj->getOauthAccessToken();
				if (!$json) {
					unset($_SESSION['wx_redirect']);
					die('获取用户授权失败，请重新确认');
				}
				$_SESSION['open_id'] = $this->open_id = $json["openid"];
				$access_token = $json['access_token'];
				$_SESSION['user_token'] = $access_token;
				$_SESSION['token_time'] = time();
				$userinfo = $we_obj->getUserInfo($this->open_id);
				if ($userinfo && !empty($userinfo['nickname'])) {
					$this->wxuser = array(
							'open_id'=>$this->open_id,
							'nickname'=>$userinfo['nickname'],
							'sex'=>intval($userinfo['sex']),
							'location'=>$userinfo['province'].'-'.$userinfo['city'],
							'avatar'=>$userinfo['headimgurl']
					);
				} elseif (strstr($json['scope'],'snsapi_userinfo')!==false) {
					$userinfo = $we_obj->getOauthUserinfo($access_token,$this->open_id);
					if ($userinfo && !empty($userinfo['nickname'])) {
						$this->wxuser = array(
								'open_id'=>$this->open_id,
								'nickname'=>$userinfo['nickname'],
								'sex'=>intval($userinfo['sex']),
								'location'=>$userinfo['province'].'-'.$userinfo['city'],
								'avatar'=>$userinfo['headimgurl']
						);
					} else {
						return $this->open_id;
					}
				}
				if ($this->wxuser) {
					$_SESSION['wxuser'] = $this->wxuser;
					$_SESSION['open_id'] =  $json["openid"];
					unset($_SESSION['wx_redirect']);
					return $this->open_id;
				}
				//$scope = 'snsapi_userinfo';
			}
			if ($scope=='snsapi_base') {
				$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$_SESSION['wx_redirect'] = $url;
			} else {
				$url = $_SESSION['wx_redirect'];
			}
			if (!$url) {
				unset($_SESSION['wx_redirect']);
				die('获取用户授权失败');
			}
			$oauth_url = $we_obj->getOauthRedirect($url,"wxbase",$scope);
			header('Location: ' . $oauth_url);
		}
	}
		
	public function ts(){
		var_dump("_SESSION['wx_redirect']".session('wx_redirect'));
		var_dump("open_id".session('open_id'));
	}
}